package ylsb.service;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ylsb.dao.HaocaijieguoMapper;
import ylsb.dao.YltpJieguoMapper;
import ylsb.model.*;
import ylsb.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class HaocaijieguoService {

    @Resource
    HaocaijieguoMapper ddMapper;

    @Resource
    HaocaiyinjinService ddHaocaiyinjinService;

    @Resource
    YltpuserService ddYltpuserService;

    public Integer checkCountByPinzhongandUserId(String ppPinzhong,String ppuserId){
        HaocaijieguoExample mmExample=new HaocaijieguoExample();
        HaocaijieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andPinzhongEqualTo(ppPinzhong).andUseridEqualTo(ppuserId).andResultEqualTo("1");
        Object mmCount=ddMapper.selectByExample(mmExample).size();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Haocaijieguo checkexistByUserId(String ppPinzhong,String ppPinpai,String ppuserId){
        HaocaijieguoExample mmExample=new HaocaijieguoExample();
        HaocaijieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andPinzhongEqualTo(ppPinzhong).andPinpaiEqualTo(ppPinpai).andUseridEqualTo(ppuserId);
        return ddMapper.selectByExample(mmExample).size()>0? ddMapper.selectByExample(mmExample).get(0): null;
    }

    public List<Map<String,Object>> findByxiangmuId(String mmXiangmuId){
        return ddMapper.findByxiangmuId(mmXiangmuId);
    }


    public List<Map<String,Object>> getAgreeListByXiangMuIdAndPinZhong(String mmXiangmuId,int mmPinzhong){
        return  ddMapper.getAgreeListByXiangMuIdAndPinZhong(mmXiangmuId,mmPinzhong);
    }

    public boolean add(Haocaijieguo ppHaocaijieguo){
        return ddMapper.insert(ppHaocaijieguo)==1;
    }
    public boolean modify(Haocaijieguo ppHaocaijieguo){
        return ddMapper.updateByPrimaryKeySelective(ppHaocaijieguo)==1;
    }

    public Integer getHaocaiduibiaoResultCount(String mmxiangmuId,String mmuserId,String mmresult){
        Object mmCount = ddMapper.getHaocaiduibiaoResultCount(mmxiangmuId,mmuserId,mmresult);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public void daochuqianzibiao(@RequestParam("xiangmuid") String ppxiangmuid,
                                 @RequestParam("userid") String ppuserid,
                                 HttpServletRequest request, HttpServletResponse response){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddHaocaiyinjinService.findDaochuPinzhongAll(ppxiangmuid);
        YltpUser mmuser=ddYltpuserService.findOne(ppuserid);
        String mmusername=mmuser.getUsername();
        OutputStream os=null;
        WritableWorkbook wbook=null;
        try
        {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = "耗材"+mmusername+"委员结果表打印签字"+ StringHandle.tiemString(); //文件名
            String tmptitle = "医用耗材引进表决表"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,500); //第一行高度
            wsheet.mergeCells(0, 0, 14, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,15); //宽度
            wsheet.setColumnView(2,15); //宽度
            wsheet.setColumnView(3,10); //宽度
            wsheet.setColumnView(4,20); //宽度
            wsheet.setColumnView(5,10); //宽度
            wsheet.setColumnView(6,10); //宽度
            wsheet.setColumnView(7,10); //宽度
            wsheet.setColumnView(8,15); //宽度
            wsheet.setColumnView(9,10); //宽度
            wsheet.setColumnView(10,10); //宽度
            wsheet.setColumnView(11,10); //宽度
            wsheet.setColumnView(12,15); //宽度
            wsheet.setColumnView(13,15); //宽度
            wsheet.setColumnView(14,20); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            wsheet.addCell(new Label(0, 1, "序号",wcfFC));
            wsheet.addCell(new Label(1, 1, "品种",wcfFC));
            wsheet.addCell(new Label(2, 1, "品牌",wcfFC));
            wsheet.addCell(new Label(3, 1, "型号拆分",wcfFC));
            wsheet.addCell(new Label(4, 1, "平台编码",wcfFC));
            wsheet.addCell(new Label(5, 1, "注册证名称",wcfFC));
            wsheet.addCell(new Label(6, 1, "规格",wcfFC));
            wsheet.addCell(new Label(7, 1, "型号",wcfFC));
            wsheet.addCell(new Label(8, 1, "注册证号",wcfFC));
            wsheet.addCell(new Label(9, 1, "生产商",wcfFC));
            wsheet.addCell(new Label(10, 1, "计价单位",wcfFC));
            wsheet.addCell(new Label(11, 1, "平台情况",wcfFC));
            wsheet.addCell(new Label(12, 1, "平台单价（元）",wcfFC));
            wsheet.addCell(new Label(13, 1, "表决意见",wcfFC));
            wsheet.addCell(new Label(14, 1, "表决人",wcfFC));

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmpinzhongKaishihang=2;
            int mmpinpaiKaishihang=2;
            int mmxinghaoKaishihang=2;

            for(int i=0;i<count;i++){
                Map<String,Object> mmPinzhongMap=mmList.get(i);

                String mmPinzhongOne = mmPinzhongMap.get("pinzhong") == null ? "" : mmPinzhongMap.get("pinzhong").toString();
                int mmHebingshu = 0;

                List<Map<String,Object>> mmPinpailist = ddHaocaiyinjinService.findPinpaiByPinzhong(mmPinzhongOne);
                if (mmPinpailist.size()>0){
                    mmHebingshu = mmPinpailist.size()-1;
                }
                List<Map<String,Object>> mmPinpaiFenlei = ddHaocaiyinjinService.findPinpaiFenlei(mmPinzhongOne);
                for (int j = 0; j < mmPinpaiFenlei.size(); j++) {
                    int s=0;
                    Map<String,Object> mmPinpaiMap = mmPinpaiFenlei.get(j);
                    int mmPinpaiHebingshu = 0;
                    if (mmPinpaiMap!=null){
                        String mmPinpaiOne = mmPinpaiMap.get("pinpai") == null ? "" : mmPinpaiMap.get("pinpai").toString();
                        List<Map<String,Object>> mmXinhaoList = ddHaocaiyinjinService.findXinghaoJieguoByPinpai(mmPinzhongOne,mmPinpaiOne,ppxiangmuid,ppuserid);
                        if (mmXinhaoList.size()>0){
                            mmPinpaiHebingshu = mmXinhaoList.size()-1;
                        }
                        for (int k = 0; k < mmXinhaoList.size(); k++) {
                            Map<String, Object> mmXinhaoMap = mmXinhaoList.get(k);
                            if (mmXinhaoMap != null) {
                                String mmXuhao = mmXinhaoMap.get("ordernumber")==null ? "" :mmXinhaoMap.get("ordernumber").toString();
                                String mmPinzhong=mmXinhaoMap.get("pinzhong")==null ? "" :mmXinhaoMap.get("pinzhong").toString();
                                String mmpinpai=mmXinhaoMap.get("pinpai")==null ? "" :mmXinhaoMap.get("pinpai").toString();
                                String mmxinghaochaifen=mmXinhaoMap.get("xinghaochaifen")==null ? "" :mmXinhaoMap.get("xinghaochaifen").toString();
                                String mmpingtaibianma=mmXinhaoMap.get("pingtaibianma")==null ? "" :mmXinhaoMap.get("pingtaibianma").toString();
                                String mmzhucezhengmingcheng=mmXinhaoMap.get("zhucezhengmingcheng")==null ? "" :mmXinhaoMap.get("zhucezhengmingcheng").toString();
                                String mmguige=mmXinhaoMap.get("guige")==null ? "" :mmXinhaoMap.get("guige").toString();
                                String mmxinghao=mmXinhaoMap.get("xinghao")==null ? "" :mmXinhaoMap.get("xinghao").toString();
                                String mmzhucezhenghao=mmXinhaoMap.get("zhucezhenghao")==null ? "" :mmXinhaoMap.get("zhucezhenghao").toString();
                                String mmshengchanshang=mmXinhaoMap.get("shengchanshang")==null ? "" :mmXinhaoMap.get("shengchanshang").toString();
                                String mmjijiadanwei=mmXinhaoMap.get("jijiadanwei")==null ? "" :mmXinhaoMap.get("jijiadanwei").toString();
                                String mmpingtaiqingkuang=mmXinhaoMap.get("pingtaiqingkuang")==null ? "" :mmXinhaoMap.get("pingtaiqingkuang").toString();
                                String mmpingtaidanjia=mmXinhaoMap.get("pingtaidanjia")==null ? "" :mmXinhaoMap.get("pingtaidanjia").toString();
                                String mmnanjingshiyibaoma=mmXinhaoMap.get("nanjingshiyibaoma")==null ? "" :mmXinhaoMap.get("nanjingshiyibaoma").toString();
                                String mmgongnengyongtu=mmXinhaoMap.get("gongnengyongtu")==null ? "" :mmXinhaoMap.get("gongnengyongtu").toString();
                                String mmresult=mmXinhaoMap.get("result")==null ? "" :mmXinhaoMap.get("result").toString();
                                String mmbiaojueren=mmXinhaoMap.get("username")==null ? "" :mmXinhaoMap.get("username").toString();
                                String mmyijian="";
                                if("1".equals(mmresult)){
                                    mmyijian="同意";
                                }else if("0".equals(mmresult)){
                                    mmyijian="不同意";
                                }else {
                                    mmyijian="未表决";
                                }
                                wsheet.addCell(new Label(0, mmxinghaoKaishihang,mmXuhao,wcfFC));
                                wsheet.addCell(new Label(3, mmxinghaoKaishihang, mmxinghaochaifen,wcfFC));
                                wsheet.addCell(new Label(4, mmxinghaoKaishihang,mmpingtaibianma,wcfFC));
                                wsheet.addCell(new Label(5, mmxinghaoKaishihang,mmzhucezhengmingcheng,wcfFC));
                                wsheet.addCell(new Label(6, mmxinghaoKaishihang,mmguige,wcfFC));
                                wsheet.addCell(new Label(7, mmxinghaoKaishihang,mmxinghao,wcfFC));
                                wsheet.addCell(new Label(8, mmxinghaoKaishihang,mmzhucezhenghao,wcfFC));
                                wsheet.addCell(new Label(9, mmxinghaoKaishihang,mmshengchanshang,wcfFC));
                                wsheet.addCell(new Label(10, mmxinghaoKaishihang,mmjijiadanwei,wcfFC));
                                wsheet.addCell(new Label(11, mmxinghaoKaishihang,mmpingtaiqingkuang,wcfFC));
                                wsheet.addCell(new Label(12, mmxinghaoKaishihang,mmpingtaidanjia,wcfFC));
                                wsheet.addCell(new Label(13, mmxinghaoKaishihang,mmyijian,wcfFC));
                                wsheet.addCell(new Label(14, mmxinghaoKaishihang,mmbiaojueren,wcfFC));
                                mmxinghaoKaishihang = mmxinghaoKaishihang+1;
                            }
                        }
                        wsheet.addCell(new Label(2, mmpinpaiKaishihang, mmPinpaiOne,wcfFC));
                        wsheet.mergeCells(2, mmpinpaiKaishihang, 2, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(13, mmpinpaiKaishihang, 13, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(14, mmpinpaiKaishihang, 14, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        mmpinpaiKaishihang = mmpinpaiKaishihang+mmPinpaiHebingshu+1;
                    }
                }
                wsheet.addCell(new Label(1, mmpinzhongKaishihang, mmPinzhongOne,wcfFC));
                wsheet.mergeCells(1, mmpinzhongKaishihang, 1, mmpinzhongKaishihang+mmHebingshu);
                mmpinzhongKaishihang = mmpinzhongKaishihang+mmHebingshu+1;

            }
            wbook.write(); // 写入文件
            wbook.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }



}
